#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	unsigned long mem_size = atoi(argv[1]) * 1024;
	int fd = open("/dev/sda", O_RDONLY);
	struct timespec start_time, end_time;
	double response_time;
	if(fd < 0)
	{
		fprintf(stderr, "\nError accessing hard disk");
		return 1;
	}
	char *buffer = (char *)malloc(mem_size);
	if(!buffer)
	{
		fprintf(stderr, "\nUnable to allocate buffer");
		return 1;
	}
	ssize_t num_read;
	int i;
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
	for(i = 0; i < 1000; i++)
	{
		num_read = read(fd, buffer, mem_size);
		if(num_read != mem_size)
		{
			fprintf(stderr, "\nNumber of bytes read = %ld", num_read);
		}
	}
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
	response_time = end_time.tv_sec * 1000000000 + end_time.tv_nsec - (start_time.tv_sec * 1000000000 + start_time.tv_nsec);
	response_time /= 1000;
	printf("\nMeasured Response Time = %lf\n", response_time);
	free(buffer);
	buffer = NULL;
	close(fd);
	return 0;
}
